package com.example.common.dto;

import com.example.common.enums.ErrorCodeEnum;
import lombok.Data;

/**
 * 返回结果
 */
public class ApiResponse<T> {
    private int code;
    private String message;
    private T data;

    // 成功
    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(ErrorCodeEnum.SUCCESS.getCode());
        response.setMessage(ErrorCodeEnum.SUCCESS.getMessage());
        response.setData(data);
        return response;
    }

    //失败，返回枚举类中的错误信息
    public static ApiResponse<Void> error(ErrorCodeEnum errorCode) {
        ApiResponse<Void> response = new ApiResponse<>();
        response.setCode(errorCode.getCode());
        response.setMessage(errorCode.getMessage());
        return response;
    }

    //失败，返回自定义错误信息
    public static ApiResponse<Void> error(int code, String message) {
        ApiResponse<Void> response = new ApiResponse<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return code == ErrorCodeEnum.SUCCESS.getCode();
    }
}